package external_libraries;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JPanel;

public class PrintUIWindow implements Printable, ActionListener {

    // private JFrame frameToPrint;
    private JPanel panelToPrint;

    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {

        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX() + 40, pf.getImageableY() + 60);
        g2d.scale(0.58, 0.58);

        // frameToPrint.print(g);
        panelToPrint.print(g);

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {

            }
        }
    }

    public PrintUIWindow(JPanel p) {
        panelToPrint = p;
    }

}
